package com.cn.dao;

import com.cn.BaseDao.BaseDao;
import com.cn.daoIntface.UserDaoIntface;
import com.cn.entity.UserEntity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.util.List;

public class Userdao extends BaseDao implements UserDaoIntface {
    @Override
    public UserEntity queryByPhone(UserEntity userEntity, String type) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        String sql = null;
        UserEntity UserEntity = null;
        if (type.equals("login")) {
            sql = "select * from user where phone=? and password=? LIMIT 1";
            UserEntity = (UserEntity) this.executeQueryOne(sql, UserEntity.class, userEntity.getPhone(), userEntity.getPassword());
        } else if (type.equals("zc")) {
            sql = "select * from user where phone=?  LIMIT 1";
            UserEntity = (UserEntity) this.executeQueryOne(sql, UserEntity.class, userEntity.getPhone());
        }else if(type.equals("eq")){
            sql = "select * from user where user_id=?  LIMIT 1";
            UserEntity = (UserEntity) this.executeQueryOne(sql, UserEntity.class, userEntity.getUser_id());
        }
        return UserEntity;
    }

    @Override
    public int addUser(UserEntity userEntity) throws SQLException, ClassNotFoundException {
        Long millisecond = Instant.now().toEpochMilli();
        String random = "" + (int) ((Math.random() * 9 + 1) * 1000);
        String s=millisecond+random+userEntity.getPhone();
        String sql = "INSERT INTO user(user_id,phone,password,date_in) VALUES(?,?,?,now())";
        return this.executeUpdate(sql, s, userEntity.getPhone(), userEntity.getPassword());
    }

    @Override
    public int updateUser(UserEntity userEntity, String type) throws SQLException, ClassNotFoundException {
        String sql = null;
        int rows = 0;
        if (type.equals("updatephone")) {
            sql = "UPDATE user SET password= ? where phone=?";
            rows = this.executeUpdate(sql, userEntity.getPassword(), userEntity.getPhone());
        } else if (type.equals("updateAll")) {
            sql = "update user set stat=?,sex=?,excellent=?,name=?,id_card=? where phone=? ";
            rows = this.executeUpdate(sql, userEntity.getStat(), userEntity.getSex(), userEntity.getExcellent(), userEntity.getName(), userEntity.getId_card(), userEntity.getPhone());
        }
        return rows;
    }

    @Override
    public List<UserEntity> queryAll(Integer currentPageNo, Integer pageSize) throws SQLException, ClassNotFoundException {
        return null;
    }

    @Override
    public List<UserEntity> queryAll() {
        return null;
    }

    @Override
    public String maxid() throws SQLException, ClassNotFoundException {
        return this.getStringNumber(1, "U", "00000", "user_id", "user");
    }

    @Override
    public Object tableToClass(ResultSet rs) throws Exception {
        return null;
    }
}
